angular.module("workBusTicketApp",['ui.bootstrap','httpService']).
controller("workBusTicketCtrl",['$scope','$http','$uibModal','$timeout','workBusesService','routesService','workTicketService',
function($scope,$http,$modal,$timeout,workBusesService,routesService,workTicketService){

 $scope.selectedBusOrder={};
 $scope.getBuses=function(selectedBusOrder){
     busesService.getInfoByBusOrderId({busOrderId:selectedBusOrder.id}).then(function(response){
          $scope.buses=response;
          $scope.busindex=0;
          $scope.busSelected(0,$scope.buses[0]);
     });
     busOrderService.getById({id:selectedBusOrder.id}).then(function(response){
          $scope.busOrder=response;
     });
 };
 $scope.refreshBuses=function(){
       workBusesService.getValidate().then(function(response){
          $scope.buses=response;
           $scope.busindex=0;
          $scope.busSelected(0,$scope.buses[0]);
     });  
 };
//$scope.getBuses();
$scope.refreshBuses();
$scope.busindex=0;
$scope.busSelected=function(index,bus){
    $scope.busindex=index;
    $scope.bus=bus;
    $scope.ticketInfoes=[];
    $scope.seats=[];
     var rowCounts = parseInt($scope.bus.seatsCount / 4);
    //初始化车位
    var initialSeats=function () {
      $scope.seats=[];
      for(var i=0;i<parseInt($scope.bus.seatsCount/4);i++){
        var seat={};
        seat.num1=4*i+1;
        seat.num2=4*i+2;
        seat.num3=4*i+3;
        seat.num4=4*i+4;

        seat.name1='';
        seat.name2='';
        seat.name3='';
        seat.name4='';
        
        seat.type1=0;
        seat.type2=0;
        seat.type3=0;
        seat.type4=0;
        
        angular.forEach($scope.ticketInfoes,function (value) {
          if(value.seatNumber== seat.num1){
            seat.name1=value.seatName;
            seat.type1=value.auditStatus;
          }
          if(value.seatNumber== seat.num2){
            seat.name2=value.seatName;
            seat.type2=value.auditStatus;
          }
          if(value.seatNumber== seat.num3){
            seat.name3=value.seatName;
            seat.type3=value.auditStatus;
          }
          if(value.seatNumber== seat.num4){
            seat.name4=value.seatName;
            seat.type4=value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }

     var yushu = $scope.bus.seatsCount % 4;
      if (yushu == 3) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;
        seat.num3 = 3 + rowCounts * 4;
        seat.name1 = '';
        seat.name2 = '';
        seat.name3 = '';
        seat.type1 = 0;
        seat.type2 = 0;
        seat.type3 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }
          if (value.seatNumber == seat.num3) {
            seat.name3 = value.seatName;
            seat.type3 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
      if (yushu == 2) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.num2 = 2 + rowCounts * 4;

        seat.name1 = '';
        seat.name2 = '';

        seat.type1 = 0;
        seat.type2 = 0;

        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
          if (value.seatNumber == seat.num2) {
            seat.name2 = value.seatName;
            seat.type2 = value.auditStatus;
          }

        });
        $scope.seats.push(seat);
      }
      if (yushu == 1) {
        var seat = {};
        seat.num1 = 1 + rowCounts * 4;
        seat.name1 = '';
        seat.type1 = 0;
        angular.forEach($scope.ticketInfoes, function (value) {
          if (value.seatNumber == seat.num1) {
            seat.name1 = value.seatName;
            seat.type1 = value.auditStatus;
          }
        });
        $scope.seats.push(seat);
      }
    };
     workTicketService.getInfoByBusId({busId:$scope.bus.id}).then(function (response) {
        $scope.ticketInfoes=response;
        $scope.seats=[];
        initialSeats();
      },function (response) {
        initialSeats();
      });
    
};
    $scope.ticketSeat=function(seatNum){
        $scope.seatedNum=seatNum;
        $modal.open({
          templateUrl:'ticketSeatModel',
          controller:'ticketSeatCtrl',
          scope:$scope,
          backdrop:'static'
      }); 
    };
}]).
controller("ticketSeatCtrl",['$scope','$uibModalInstance','workBusesService','workTicketService','routesService',
function($scope,$uibModalInstance,workBusesService,workTicketService,routesService){   
    $scope.submit=function(){
        if($scope.seatName==undefined||$scope.seatName.length>3||$scope.seatName.length<2){
            alert("乘坐人姓名长度应该是在2-3之间!");
            return;
        }
        $scope.loading=true;
      var ticketInfo={};
      ticketInfo.phone=13600000200;
      ticketInfo.password='liushui';
      ticketInfo.seatNumber= $scope.seatedNum;
      ticketInfo.seatName=$scope.seatName;
      ticketInfo.busId=$scope.bus.id;
      ticketInfo.staffId=3000;
        workTicketService.adminInsert(ticketInfo).then(function(response){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result=response.message;
            $scope.refreshBuses($scope.selectedBusOrder);
            $scope.busSelected($scope.busindex,$scope.bus);
        },function(response){
            $scope.loading=false;
            $scope.submitted=true;
            $scope.result=response.message;
        });
    };
    $scope.close=function(){
        $uibModalInstance.close();
    };
}]);